﻿Imports VendasDAL
Public Class PlacaBLL
    Dim objDAL As PlacaDAL
#Region "Propriedades"
    Property Descricao As String
    Property Ano As String
    Property Cor As String
    Property Chassi As String
    Property Status As Integer
    Property Codigo As Integer
    Property CliCodigo As Integer
    Property CarCodigo As Integer
    Property CliDescricao As String
    Property CarDescricao As String
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer, ano As String, cor As String, chassi As String, cliente As Integer, carro As Integer)
        Me.Ano = ano
        Me.Chassi = chassi
        Me.Codigo = codigo
        Me.Cor = cor
        Me.Descricao = descricao
        Me.Status = status
        Me.CliCodigo = cliente
        Me.CarCodigo = carro
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Boolean
        Try
            If (Descricao <> "" And Ano <> "" And CliCodigo > 0 And CarCodigo > 0) Then
                If (Ano.Length = 4) Then
                    If (Chassi <> "") Then If Chassi.Length < 17 Then Throw (New Exception("Chassi inválido, deve conter 17 caracteres"))
                    If (Descricao.Length = 8) Then
                        If (objfuncoes.ValidaReplica(Me.Descricao, 0, "Vendas.Ofi_Placa", "pla_codigo", "pla_descricao").Rows.Count = 0) Then
                            objDAL = New PlacaDAL
                            Me.Codigo = objDAL.Inserir(Me.Descricao, Me.Ano, Me.Cor, Me.Chassi, Me.Status, Me.CliCodigo, Me.CarCodigo)
                        Else : Throw New Exception("Placa já cadastrada")
                        End If
                    Else : Throw New Exception("Placa inválida, deve conter 7 caracteres ex:(XXX4567)")
                    End If
                Else : Throw New Exception("Ano inválido, deve conter 4 caracteres ex:(1234)")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            objDAL = New PlacaDAL
            If (Descricao <> "" And Ano <> "" And CliCodigo > 0 And CarCodigo > 0) Then
                If (Ano.Length = 4) Then
                    If (Chassi <> "") Then If Chassi.Length < 17 Then Throw (New Exception("Chassi inválido, deve conter 17 caracteres"))
                    If (Descricao.Length = 8) Then
                        If (objfuncoes.ValidaReplica(Me.Descricao, Me.Codigo, "Vendas.Ofi_Placa", "pla_codigo", "pla_descricao").Rows.Count = 0) Then
                            objDAL = New PlacaDAL
                            Me.Codigo = objDAL.Alterar(Me.Codigo, Me.Descricao, Me.Ano, Me.Cor, Me.Chassi, Me.Status, Me.CliCodigo, Me.CarCodigo)
                        Else : Throw New Exception("Placa já cadastrada")
                        End If
                    Else : Throw New Exception("Placa inválida")
                    End If
                Else : Throw New Exception("Ano inválido")
            End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Excluir"
    Function Excluir(ByVal Codigo As Integer) As Boolean
        Dim ret As Integer
        Try
            objDAL = New PlacaDAL
            ret = ObjDAL.Deletar(Codigo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New PlacaDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New PlacaDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Ano = Integer.Parse(tab.Rows(0)("pla_ano").ToString)
                Me.Chassi = tab.Rows(0)("pla_chassi").ToString
                Me.Codigo = tab.Rows(0)("pla_codigo").ToString
                Me.Cor = tab.Rows(0)("pla_cor").ToString
                Me.Descricao = tab.Rows(0)("pla_descricao").ToString
                Me.Status = tab.Rows(0)("sta_codigo").ToString
                Me.CliCodigo = tab.Rows(0)("cli_codigo").ToString
                Me.CarCodigo = tab.Rows(0)("crr_codigo").ToString
                Me.CliDescricao = tab.Rows(0)("cli_nome").ToString
                Me.CarDescricao = tab.Rows(0)("crr_descricao").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Ano = Nothing
        Me.Chassi = Nothing
        Me.Codigo = Nothing
        Me.Cor = Nothing
        Me.Descricao = Nothing
        Me.Status = Nothing
        Me.CliCodigo = Nothing
        Me.CarCodigo = Nothing
    End Sub
#End Region
End Class
